
const tab_nav =document.querySelector('.tab-nav')
const pane = document.querySelectorAll('.tab-pane')

tab_nav.addEventListener('click',function(e){
	if(e.target.tagName === 'A'){
		tab_nav.querySelector('.active').classList.remove('active')
		
		e.target.classList.add('active')
		
		for(let i =0;i<pane.length;i++){
			pane[i].style.display ='none'
		}
		pane[e.target.dataset.id].style.display = 'block'
	}
})

//密码验证
const password = document.querySelector('[name=password]')
password.addEventListener('change',verifypassword)
	
function verifypassword(){
  const span =password.nextElementSibling
  const reg =/^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{6,20}$/
  if(!reg.test(password.value)){
    span.innerHTML ='6-20位,至少包含字母、数字、特殊字符'
    return false
  }
  span.innerHTML = ''
  return true
}

const go_login = document.querySelector('.dl')
const agree = document.querySelector('[name=agree]')
const user_account =document.querySelector('[name=username]')
go_login.addEventListener('click',function(e){
	e.preventDefault()
	if(!agree.checked){
		return alert('请勾选同意协议')
	}

  var data = {"username":user_account.value,"userphone":user_account.value,"userpassword":password.value}

  $.ajax({
    type: "post",  //数据提交方式（post/get）
    url: "http://127.0.0.1:8088/api/login",  //提交到的url
    data: data,//提交的数据
    dataType: "json",//返回的数据类型格式
    xhrFields: {
      withCredentials: true
     },
     crossDomain: true,
        xhrFields: {
          withCredentials: true // 携带跨域cookie
      },
    success: function(res) {
      if (res.status == 400){
          document.querySelector('.tankuang').style.display = 'block',
          document.querySelector('#layer_msg').style.color = 'red'
          $('#layer_msg').text('账号不存在,请先注册')
          setTimeout(function(){
              document.querySelector('.tankuang').style.display = 'none'
              
          },1500)
      } else if (res.status == 401) {
          document.querySelector('.tankuang').style.display = 'block',
          document.querySelector('#layer_msg').style.color = 'red'
          $('#layer_msg').text('密码错误,请重新登录')
          setTimeout(function(){
              document.querySelector('.tankuang').style.display = 'none'
          },1500)
      } else if (res.status == 200) {
        document.querySelector('.tankuang').style.display = 'block',
          document.querySelector('#layer_msg').style.color = 'green'
          $('#layer_msg').text('登录成功，正在为您跳转页面......')
          setTimeout(function(){
              document.querySelector('.tankuang').style.display = 'none'
              sessionStorage.setItem('xtx-uname',res.account)
              location.href = 'index.html'
          },1000)
      }
  }
  });
})



